<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // // 方法一：
    // //父构造函数
    // function Person(name, age) {
    //   this.name = name
    //   this.age = age
    // }
    // Person.prototype.sayHi = function () {
    //   console.log(`我是${this.name},今年${this.age}`)
    // }
    // //子构造函数
    // function Teacher(name, age, coures) {
    //   //改变Person的this指向，实现给Teacher属性赋值，从而实现属性继承
    //   Person.call(this, name, age)
    //   this.coures = coures
    // }
    // Teacher.prototype.__proto__ = Person.prototype
    // Teacher.prototype.teach = function () {
    //   console.log(`我是${this.name},我在上${this.coures}课`)
    // }
    // const zp = new Teacher('zp', 18, '前端')
    // console.log(zp)
    // const zp1 = new Person('zp1', 18, '前端')
    // console.log(zp1)
    // zp.teach()
    // zp.sayHi()
    // // zp1.teach()//报错
    // console.log(zp.__proto__.constructor === Teacher)//true


    // 方法二：
    //父构造函数
    function Person(name, age) {
      this.name = name
      this.age = age
    }
    Person.prototype.sayHi = function () {
      console.log(`我是${this.name},今年${this.age}`)
    }
    //子构造函数
    function Teacher(name, age, coures) {
      //改变Person的this指向，实现给Teacher属性赋值，从而实现属性继承
      Person.call(this, name, age)
      this.coures = coures
    }
    // Teacher.prototype.__proto__ = Person.prototype
    Teacher.prototype = Object.create(Person.prototype)
    Teacher.prototype.constructor = Teacher
    Teacher.prototype.teach = function () {
      console.log(`我是${this.name},我在上${this.coures}课`)
    }
    const zp = new Teacher('zp', 18, '前端')
    console.log(zp)
    const zp1 = new Person('zp1', 18, '前端')
    console.log(zp1)
    zp.teach()
    zp.sayHi()
    // zp1.teach()//报错
    console.log(zp.__proto__.constructor === Teacher)//true
  </script>
</body>

</html>